package weka.classifiers.neural.backpropagation;

import java.io.Serializable;
import java.util.Random;

public class Neuron implements Serializable
{	
	
	private static final long serialVersionUID = -1107151211535315955L;
	//神经元的输入偏倚量,输入层为0
	private double bias;
	//神经元净输入
	private double input;
	//神经元净输出
	private double output;
	//神经元传播误差
	private double err;	
	//神经元来自上一层节点的权重，大小为上一层节点数目
	private double[] inputWeights;
		
	//在网络拓扑图上的坐标,layerIndex为层数，innerIndex为层内序号
	//layerIndex=0表示输入层，layerIndex=-1表示输出层
	private int layerIndex;
	private int innerIndex;
	
	/*
	 * @param lastLayerNum	上一层节点数目
	 * @param layerIndex   	当前节点所处层数
	 * @param innerIndex   	当前节点层内序号
	 */
	public Neuron(int lastLayerNum,int layerIndex,int innerIndex) {
		if(random==null)
			random=new Random();		
		inputWeights=new double[lastLayerNum];
		this.layerIndex=lastLayerNum;
		this.innerIndex=innerIndex;
		
		randomInit();
	}
	private void randomInit()
	{
		//除了输入层，都有输入偏倚量
		if(layerIndex!=0)
			bias=random.nextDouble();
		for(int i=0;i<inputWeights.length;i++)
		{
			inputWeights[i]=random.nextDouble();
		}
	}
	public double getBias() {
		return bias;
	}
	public void setBias(double bias) {
		this.bias = bias;
	}
	public double getInput() {
		return input;
	}
	public void setInput(double input) {
		this.input = input;
	}
	public double getOutput() {
		return output;
	}
	public void setOutput(double output) {
		this.output = output;
	}
	public double getErr() {
		return err;
	}
	public void setErr(double err) {
		this.err = err;
	}
	public double[] getInputWeights() {
		return inputWeights;
	}
	public void setInputWeights(double[] inputWeights) {
		this.inputWeights = inputWeights;
	}
	public int getLayerIndex() {
		return layerIndex;
	}
	public void setLayerIndex(int layerIndex) {
		this.layerIndex = layerIndex;
	}
	public int getInnerIndex() {
		return innerIndex;
	}
	public void setInnerIndex(int innerIndex) {
		this.innerIndex = innerIndex;
	}
	
	private static Random random;
	public static void initRandom(long seed)
	{
		if(random==null)
			random=new Random(seed);
	}
}